package com.cmii.sjw.department.control.jsc.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cmii.sjw.department.control.jsc.mapper.RoleMapper;
import com.cmii.sjw.department.control.jsc.mapper.UserMapper;
import com.cmii.sjw.department.control.jsc.mapper.UserRoleMapper;
import com.cmii.sjw.department.control.jsc.model.User;
import com.cmii.sjw.department.control.jsc.model.entity.Role;
import com.cmii.sjw.department.control.jsc.model.entity.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 自定义权限加载接口实现类
 * @Author jfh
 * @Date 2025-02-28 周五 10:45
 * @Version 1.0
 */
@Component    // 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserId, loginId)
                .eq(User::getStatus, 0));
//        User user = (User) StpUtil.getSession().get(loginId.toString());
        // 本 list 仅做模拟，实际项目中要根据具体业务逻辑来查询权限
        List<String> list = new ArrayList<String>();
        if (ObjectUtils.isEmpty(user)) {
            return list;
        }
        // 市级权限
        if (user.getPermission().equals("1")) {
            list.add("1");
            list.add("2");
            list.add("3");
            list.add("4");
        }
        // 区级权限
        if (user.getPermission().equals("2")) {
            list.add("2");
            list.add("4");
        }
        // 高校权限
        if (user.getPermission().equals("3")) {
            list.add("3");
        }
        // 学校权限
        if (user.getPermission().equals("4")) {
            list.add("4");
        }

        return list;
    }


    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 本 list 仅做模拟，实际项目中要根据具体业务逻辑来查询角色
        List<String> list = new ArrayList<String>();

        Set<Long> roles = userRoleMapper.selectList(new LambdaQueryWrapper<UserRole>() .eq(UserRole::getUserId, loginId))
                .stream().map(UserRole::getRoleId).collect(Collectors.toSet());
        if (roles.isEmpty()){
            return list;
        }
        list = roleMapper.selectList(new LambdaQueryWrapper<Role>().eq(Role::getStatus, 0).in(Role::getId, roles))
                .stream().map(Role::getRoleCode).collect(Collectors.toList());

        return list;
    }
}
